package jdbc;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class conexion {
	
	static Connection con;

	public static Connection con() {
		try {

			/* Driver */
			Class.forName("com.mysql.jdbc.Driver");

			/* Realizar conexion */
			//con = (Connection) DriverManager.getConnection(
				//	"jdbc:mysql://172.26.0.102/tt", "tt", "tt&1");
			
			con =(Connection) DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1/tt", "root", "");

			/* Mostramos si se ha realizado bien la conexion */
			//System.out.println("Conexion realizada");

			
			/* retorna la conexion para utilizarla */
			return con;

		}
		/* Si no encuentra el driver */
		catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}

		/* Si no puede conectarse */
		catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}
	
	public static void cerrarconexion(){
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static ResultSet crearconsulta(String c) throws SQLException{
			Statement consulta=(Statement) con.createStatement();
			ResultSet rs=consulta.executeQuery(c);
			
			return rs;
	}
	
	public static void insertar(String query) throws SQLException{
		PreparedStatement insert = con.prepareStatement(query);
		insert.executeUpdate();
	}

}
